package com.java.shop.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.Map;

public class JSONUtil {

    private static ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 将map集合转成json字符串
     * @param map
     * @return
     */
    public static String mapToJson(Map<String, Object> map){
        String json = "";
        try {
            json = objectMapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return json;
    }


    /**
     * 将对象转成json字符串
     * @param object
     * @return
     */
    public static String toJson(Object object){
        String json = "";
        try {
            json = objectMapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return json;
    }


}
